<?php
namespace App\Model;
use Core\Component\Di;
use App\Vendor\StringHelper;

class Users{
    protected $db;
    protected $tableName = 'users';

    function __construct(){
        $db = Di::getInstance()->get("MYSQL");
        if($db instanceof \MysqliDb){
            $this->db = $db;
        }
    }

    public function userLogin($mobile,$password){
        if(!$mobile || !$password){
            return [];
        }
        $this->db->where("mobile", $mobile);
        $this->db->where("password", $password);
        $column = 'id as userId,mobile,username';
        $info = $this->db->getOne($this->tableName,$column);
        if(!$info){
            return [];
        }
        $info['photo']='http://www.sciclass.cn/images/teacher-head-img.png';
        return $info;
    }
    
    public function userRegister($mobile,$username,$password){
        if(!$mobile || !$username || !$password){
            return [];
        }
        $data['mobile']=$mobile;
        $data['username']=$username;
        $data['password']=$password;
        $id = $this->db->insert($this->tableName,$data);
        if(!$id){
            return [];
        }else{
            $data['userId']=$id;
            $data['photo']='http://www.sciclass.cn/images/teacher-head-img.png';
            unset($data['password']);
            return $data;
        }
    }
    
    public function getUserInfoByMobile($mobile){
        if(!$mobile){
            return [];
        }
        $this->db->where("mobile", $mobile);
        $column = 'id as userId,mobile,username';
        $info = $this->db->getOne($this->tableName,$column);
        if(!$info){
            return [];
        }
        return $info;
    }
    
    public function getUserInfoById($userId){
        if(!$userId){
            return [];
        }
        $this->db->where("id", $userId);
        $column = 'id as userId,mobile,username';
        $info = $this->db->getOne($this->tableName,$column);
        if(!$info){
            return [];
        }
        $info['photo']='http://www.sciclass.cn/images/teacher-head-img.png';
        return $info;
    }

    public function searchUserByName($key,$isFriend,$pageNum=1,$pageSize=21){
        if(!$key){
            return [];
        }
        if($isFriend){
            $this->db->having("id ",$isFriend,'not in');
        }
        $this->db->where("mobile", '%'.$key.'%','like','or');
        $this->db->where("username", '%'.$key.'%','like','or');
        $column = 'id as userId,mobile,username';
        $this->db->pageLimit = $pageSize;
        $products = $this->db->arraybuilder()->paginate($this->tableName, $pageNum,$column);
        if(!$products){
            return [];
        }
        $list['totalPage']=$this->db->totalPages;
        $list['totalCount']=$this->db->totalCount;
        foreach ($products as &$info){
            $info['photo']='http://www.sciclass.cn/images/teacher-head-img.png';
        }
        $list['users']=$products;
        return $list;
    }
}
